package com.xmutca.cloud.auth.security;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.xmutca.cloud.auth.entity.SysUser;
import com.xmutca.cloud.auth.service.SysUserService;

public class SecurityUserDetailService implements UserDetailsService {

	@Autowired
	private SysUserService userService;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUser user = userService.findByUsername(username);
		if (null == user) {
			throw new UsernameNotFoundException("username not found.");
		}
		
		//该对象存储的用户的权限字符串列表
		Collection<? extends GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		return new SecurityPrincipal(username, user.getPassword(), authorities , user.getUuid(), user.getSalt());
	}
}
